package com.koron.css2.common.utils;

import com.aspose.words.Document;
import com.aspose.words.PdfSaveOptions;
import com.aspose.words.SaveFormat;
import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;
import java.io.OutputStream;

@Slf4j
public class Word2PdfAsposeUtil {


    /**
     * word 转 pdf
     * @param inputStream word文件流
     * @param outputStream 输出pdf输出流
     * @return
     */
    public static Boolean convert2PdfStream(InputStream inputStream, OutputStream outputStream) {
        try {
            if (AsposeLicenseUtil.setWordsLicense()) {

                Document doc = new Document(inputStream);

                // insertWatermarkText(doc, "测试水印"); // 添加水印

                PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
                pdfSaveOptions.setSaveFormat(SaveFormat.PDF);
                // 设置3级doc书签需要保存到pdf的heading中
                pdfSaveOptions.getOutlineOptions().setHeadingsOutlineLevels(3);
                // 设置pdf中默认展开1级
                pdfSaveOptions.getOutlineOptions().setExpandedOutlineLevels(1);
                //doc.save(outputStream, pdfSaveOptions);
                doc.save(outputStream, SaveFormat.PDF);
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            log.error("docx转换pdf失败", e);
            return false;
        }
    }




}
